package com.example.home.house.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.house.activity.HouseDetailsActivity;
import com.example.home.house.bean.HouseBean;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class SearchhousingAdapter extends RecyclerView.Adapter<SearchhousingAdapter.MyHoldy> {
    private Context context;
    private List<HouseBean.RowsBean> list;
    private View view;

    public SearchhousingAdapter(Context context, List<HouseBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<HouseBean.RowsBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.item_house,null);
        return new MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        HouseBean.RowsBean rb = list.get(position);
        if(rb ==null){
            return;
        }
        Glide.with(context).load(ip+rb.getPic()).into(holder.apHouseimg);
        holder.apHouseimg.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.apHousetitle.setText(rb.getSourceName());
        holder.apHousetprice.setText(rb.getPrice());
        holder.apHousetcontent.setText(rb.getDescription());
        String desc = rb.getDescription();
        if(desc!=null && desc.length()>20){
            holder.apHousetcontent.setText(list.get(position).getDescription().substring(0,20)+"...");
        }else {
            holder.apHousetcontent.setText(list.get(position).getDescription());
        }
        //房源详情
        holder.reHouserapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HouseDetailsActivity.class);
                intent.putExtra("house_id",String.valueOf(list.get(position).getId()));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {

        private RelativeLayout reHouserapter;
        private ImageView apHouseimg;
        private TextView apHousetitle;
        private TextView apHousetprice;
        private TextView apHousetcontent;



        public MyHoldy(@NonNull View itemView) {
            super(itemView);

            reHouserapter = itemView.findViewById(R.id.re_houserapter);
            apHouseimg = itemView.findViewById(R.id.ap_houseimg);
            apHousetitle = itemView.findViewById(R.id.ap_housetitle);
            apHousetprice = itemView.findViewById(R.id.ap_housetprice);
            apHousetcontent = itemView.findViewById(R.id.ap_housetcontent);

        }
    }
}

